import { defineStore } from "pinia";
import type { SystemState } from "#/store";
import { BASE_FONTSIZE } from "@/utils/staticData";
import { store } from "@/store";
import { setWarehouseList } from "@/api/warehouse";
import { getSupplierList } from "@/api/supplier";
import { getDictList } from "@/api/dict";

export const useSystemStore = defineStore("system-store", {
  state: (): SystemState => ({
    rootSize: BASE_FONTSIZE, // 根节点字体大小 25大屏 23.75中屏 22.5小屏
    radio: 1, // 伸缩比例
    globalLoading: false, // 全局的loading状态
    isShowBottom: true, // 是否显示底部的公司信息
    supplierList: [], // 供应商列表
    warehouseList: [], // 仓库列表
    materialTypeList: [] // 物料类型列表
  }),
  getters: {},
  actions: {
    setLoading(flag: boolean) {
      this.globalLoading = flag;
    },
    setRootSize(rootSize) {
      this.rootSize = rootSize;
    },
    setIsShowBottom(flag: boolean) {
      this.isShowBottom = flag;
    },
    // 设置供应商列表
    setSupplierList() {
      getSupplierList().then(rsp => {
        this.supplierList = rsp.data || [];
      });
    },
    // 设置仓库列表
    setWarehouseList() {
      setWarehouseList().then(rsp => {
        this.warehouseList = rsp.data || [];
      });
    },
    getMaterialTypeList() {
      getDictList({ dictCode: "MATERIAL_TYPE" }).then((res: any) => {
        this.materialTypeList = res.data || [];
      });
    }
  }
});

// Need to be used outside the setup
export function useSystemStoreWithOut() {
  return useSystemStore(store);
}
